package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.service.wx.WxCommentService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.wx.lhh.CommentPostVo;
import com.cskaoyan.market.vo.wx.sun.CommentVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/wx/comment")
public class WxCommentController {
    @Autowired
    WxCommentService wxCommentService;
    @Autowired
    SecurityManager securityManager;

    // valueId: 1057036
    // type: 0
    // limit: 20
    // page: 1
    // showType: 0

    /**
     * 显示评论
     * @param valueId
     * @param type
     * @param limit
     * @param page
     * @param showType
     * @return
     */
    @GetMapping("list")
    public Object list(Integer valueId,Byte type,Integer limit,Integer page,Integer showType){
        // valueId=1006002&type=0&limit=20&page=1&showType=0

        List<CommentVo> list = wxCommentService.getCommentList(valueId,type,limit,page,showType);


        return ResponseUtil.okList(list);
    }
    // valueId: 1057036
    // type: 0
    // {
    //     "errno": 0,
    //         "data": {
    //     "hasPicCount": 3,
    //             "allCount": 6
    // },
    //     "errmsg": "成功"
    // }

    /**
     * 显示评论数量
     * @param valueId
     * @param type
     * @return
     */
    @GetMapping("count")
    public Object count(Integer valueId,Integer type){
       HashMap<String,Integer> countMap = wxCommentService.getCommentCount(valueId,type);
       return ResponseUtil.ok(countMap);
    }

    @PostMapping("post")
    public Object post(@RequestBody CommentPostVo postVo){
        // 获取用户id
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        postVo.setUserId(userId);
        CommentPostVo result = wxCommentService.addPostComment(postVo);

        return ResponseUtil.ok(result);
    }

}
